<!DOCTYPE html>
<html debug="true">
  <head>
    <link rel="stylesheet" type="text/css" href="../../../../ext/resources/css/ext-all.css" />

    <script type="text/javascript" src="../../../../openlayers/lib/Firebug/firebug.js"></script>
    <script type="text/javascript" src="../../../../openlayers/lib/OpenLayers.js"></script>

    <script type="text/javascript" src="../../../../ext/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="../../../../ext/ext-all-debug.js"></script>

    <script type="text/javascript">
      // Because of a bug in Firefox 2 we need to specify the MapFish base path.
      // See https://bugzilla.mozilla.org/show_bug.cgi?id=351282
      var gMfLocation = "../../../../mapfish/";
    </script>
    <script type="text/javascript" src="../../../../mapfish/MapFish.js"></script>

    <link rel="stylesheet" type="text/css" href="style.css" />

    <script type="text/javascript"><!--
        function test_constructor(t) {
            t.plan(2);
            var reader = new mapfish.widgets.data.FeatureReader({}, [
                {'name': 'foo'},
                {'name': 'bar'}
            ]);
            var fields = reader.recordType.prototype.fields;
            // 2 tests
            t.eq(fields.items.length, 2, 'number of items is correct');
            t.ok(fields.items[0].name == 'foo' &&
                 fields.items[1].name == 'bar',
                 'field values are correct');
        }
        function test_readRecords(t) {
            t.plan(9);
            // setup
            var reader, features, info, records;
            reader = new mapfish.widgets.data.FeatureReader({}, [
                {'name': 'foo'},
                {'name': 'bar'}
            ]);
            features = [
                new OpenLayers.Feature.Vector(null, {
                    'foo': 'foo_0',
                    'bar': 'bar_0'
                }),
                new OpenLayers.Feature.Vector()
            ];
            features[0].fid = 1;
            features[0].state = OpenLayers.State.INSERT;
            features[1].fid = 2;
            features[1].state = OpenLayers.State.DELETE;
            // 8 tests
            info = reader.readRecords(features);
            records = info.records;
            t.eq(info.totalRecords, 2, 'readRecords returns correct number of records');
            t.eq(records[0].get('foo'), 'foo_0', 'readRecords correctly set feature properties in record');
            t.eq(records[0].get('fid'), 1, 'readRecords correctly set feature fid in record');
            t.eq(records[0].get('state'), OpenLayers.State.INSERT, 'readRecords correctly set feature state in record');
            t.ok(records[0].get('feature') == features[0], 'readRecords correctly set feature in record');
            t.eq(records[1].get('fid'), 2, 'readRecords correctly set feature fid in record (no properties case)');
            t.eq(records[1].get('state'), OpenLayers.State.DELETE, 'readRecords correctly set feature state in record (no properties case)');
            t.ok(records[1].get('feature') == features[1], 'readRecords correctly set feature in record (no properties case)');
            // 1 test
            reader.totalRecords = 20;
            info = reader.readRecords(features);
            t.eq(info.totalRecords, 20,
                 "readRecords returns correct number of records");
        }
    --></script>

  <body>
    <div id="map"></div>
  </body>
</html>
